import pymysql
import threading

db = pymysql.connect(
    host= 'localhost',
    user='root',
    password='123456',
    database='li1_py'
)
cursor = db.cursor()
lock = threading.RLock()
#创建表结构
def createDatabase(sql):
    try:
        cursor.execute(sql)
    except Exception as e:
        print("建表出错",e)
        return 500
#新增、修改、删除数据
def modifyData(sql):
    try:
        #执行sql语句
        lock.acquire()
        cursor.execute(sql)
        db.commit()
        lock.release()
        return 200
    except Exception as e:
        print("新增、修改、删除数据报错！",e)
        #发生错误回滚
        db.rollback()
        return 500
#查数据
def queryData(sql):
    try:
        #执行sql语句
        cursor.execute(sql)
        #获取所有已记录列表
        results = cursor.fetchall()
        return results
    except:
        return 500
